package com.gt.xmall.web;

import java.util.List;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.gt.xmall.model.GtdContext;
import com.gt.xmall.service.GtdContextService;

@Controller
@RequestMapping(value = "/gtd/context/")
public class GtdContextContoller extends BaseController {
	
	@Autowired
	private GtdContextService gtdContextService;
	
	@RequestMapping(value = "list",method = RequestMethod.GET)
	public String list(Model model){
		List<GtdContext> list = gtdContextService.getGtdContexts();
		model.addAttribute("gtdContextList", list);
		return "gtd/gtdContextList";
	}
	@RequestMapping(value = "show/{id}",method = RequestMethod.GET)
	public String show(@PathVariable("id") String id,Model model){
		GtdContext gtdContext =gtdContextService.getGtdContext(new Long(id));
		model.addAttribute("gtdContext", gtdContext);
		return "gtd/gtdContextForm";
		
	}
	@RequestMapping(value="save",method = RequestMethod.POST)
	public String save(@Valid GtdContext gtdContext,RedirectAttributes redirectAttributes){
		gtdContext = gtdContextService.saveGtdContext(gtdContext);
//		redirectAttributes.addAttribute("gtdContext", gtdContext);
		return "redirect:/gtd/context/show/"+gtdContext.getId();
	} 
	@RequestMapping(value = "new",method = RequestMethod.GET)
	public String add(){
		return "gtd/gtdContextForm";
	}
}
